package com.sofwin.service.impl.contents;

import com.github.pagehelper.PageHelper;
import com.sofwin.dto.CPatandproDto;
import com.sofwin.mapper.*;
import com.sofwin.pojo.*;
import com.sofwin.service.contents.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: ssm-parent
 * @description: 专利和产品业务逻辑处理
 * @author: Mr.Xq
 * @create: 2021-04-25 11:32
 * @version:
 **/
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private CPatandproMapper patandproMapper;
    @Autowired
    private CPatandMapper patandMapper;
    @Autowired
    private CTrademarkMapper trademarkMapper;
    @Autowired
    private CAptitudesMapper aptitudesMapper;
    @Autowired
    private CResearchMapper researchMapper;

    @Override
    public CPatandproDto selectPatandproDtoByCompanyId(Integer companyId) {
        CPatandproDto dto = new CPatandproDto();

        // 查询专利和产品表，获得dto中ProductBLOBs信息
        List<CPatandproWithBLOBs> cPatandproWithBLOBs = selectCPatandpros(companyId);
        dto.setProductBLOBs(cPatandproWithBLOBs.get(0));

        // 获取companyId对应的专利和产品表id
        Integer proId = cPatandproWithBLOBs.get(0).getId();

        // 查询专利表，获得dto中patands信息
        dto.setPatands(selectCPatands(proId));

        // 查询商标表，获得dto中trademarks信息
        dto.setTrademarks(selectCTrademarks(proId));

        // 查询企业取得的资质认证和许可情况表，获得dto中aptitudess信息
        dto.setAptitudess(selectCAptitudess(proId));

        // 查询公司研发投入情况表，获得dto中researchs信息
        dto.setResearchs(selectCResearches(proId));

        return dto;
    }

    /**
    * @Description:  根据公司id查询专利和产品表，获得dto中ProductBLOBs信息
    * @Param: * @param companyId:
    * @return: * @return: java.util.List<com.sofwin.pojo.CPatandproWithBLOBs>
    * @Author: Mr.Xq
    * @Date: 2021/4/26 0026
    */
    public List<CPatandproWithBLOBs> selectCPatandpros(Integer companyId){
        // 查询专利和产品表，获得dto中ProductBLOBs信息
        CPatandproExample exam = new CPatandproExample();
        CPatandproExample.Criteria criteria = exam.createCriteria();
        criteria.andCompanyIdEqualTo(companyId);
        return patandproMapper.selectByExampleWithBLOBs(exam);
    }

    /**
    * @Description:  根据专利和产品表的id查询专利表
    * @Param: * @param proId:
    * @return: * @return: java.util.List<com.sofwin.pojo.CPatand>
    * @Author: Mr.Xq
    * @Date: 2021/4/26 0026
    */
    public List<CPatand> selectCPatands(Integer proId){
        // 查询专利表，获得dto中patands信息
        CPatandExample pExample = new CPatandExample();
        CPatandExample.Criteria criteria1 = pExample.createCriteria();
        criteria1.andPatandproIdEqualTo(proId);
        return patandMapper.selectByExample(pExample);
    }

    /**
    * @Description:  根据专利和产品表的id查询商标表
    * @Param: * @param proId:
    * @return: * @return: java.util.List<com.sofwin.pojo.CTrademark>
    * @Author: Mr.Xq
    * @Date: 2021/4/26 0026
    */
    public List<CTrademark> selectCTrademarks(Integer proId){
        // 查询商标表，获得dto中trademarks信息
        CTrademarkExample tExample = new CTrademarkExample();
        CTrademarkExample.Criteria criteria2 = tExample.createCriteria();
        criteria2.andPatandproIdEqualTo(proId);
        return trademarkMapper.selectByExample(tExample);
    }

    /**
    * @Description:  根据专利和产品表的id查询企业取得的资质认证和许可情况表
    * @Param: * @param proId:
    * @return: * @return: java.util.List<com.sofwin.pojo.CAptitudes>
    * @Author: Mr.Xq
    * @Date: 2021/4/26 0026
    */
    public List<CAptitudes> selectCAptitudess(Integer proId){
        // 查询企业取得的资质认证和许可情况表，获得dto中aptitudess信息
        CAptitudesExample aExample = new CAptitudesExample();
        CAptitudesExample.Criteria criteria3 = aExample.createCriteria();
        criteria3.andPatandproIdEqualTo(proId);
        return aptitudesMapper.selectByExample(aExample);
    }

    /**
    * @Description:  根据专利和产品表的id查询公司研发投入情况表
    * @Param: * @param proId:
    * @return: * @return: java.util.List<com.sofwin.pojo.CResearch>
    * @Author: Mr.Xq
    * @Date: 2021/4/26 0026
    */
    public List<CResearch> selectCResearches(Integer proId){
        // 查询公司研发投入情况表，获得dto中researchs信息
        CResearchExample rExample = new CResearchExample();
        rExample.setOrderByClause("research_year desc");
        CResearchExample.Criteria criteria4 = rExample.createCriteria();
        criteria4.andPatandproIdEqualTo(proId);
        // 按时间倒序查出时间最近的3条数据
        PageHelper.startPage(1,3);
        return researchMapper.selectByExample(rExample);
    }
}
